#include <cstdio>
#include <cstring>
#include <conio.h>
#include "../class/game.h"
#include "../class/ann.h"

int main(int argc, char *argv[]) {
	srand((unsigned)time(NULL));
    Ann *ann;
    if (argc == 1) {
        ann = new Ann(1000, 5000, 32, 4, 2000);
        ann->saveParam("../data/snake/0.snk");
    } else {
        ann = new Ann(argv[1], 1000);
    }
    // srand(0); // 打开这个每次游戏都是一样的
    Game game(4, 8, 2);
    game.display();
    while (1) {
        printf("Press Enter to continue, Esc to exit.\n");
        while (!_kbhit()) {}
        int hit_id = _getch();
        if (hit_id == 13) { // Enter
            bool data[32];
            game.outputData(data);
            int predict = ann->run(data);
            if (predict == -1) {
                printf("The ANN is timeout.\n");
                break;
            }
            int rst = game.go(predict);
            game.display();
            if (rst < 0)
                break;
        } else if (hit_id == 27) { // Esc
            printf("Exit.\n");
            goto End;
    }
}
    printf("Press Esc to exit.\n");
    while (1) {
        while (!_kbhit()) {}
        if (_getch() == 27) // Esc
            break;
    }
End:
    game.saveRecord("../data/record/0.rcd");
    delete ann;
    return 0;
}